package cn.justforchris.blog.controller;


import cn.justforchris.blog.domain.Article;
import cn.justforchris.blog.dto.ArticleDto;
import cn.justforchris.blog.query.articleQuery;
import cn.justforchris.blog.service.IArticleService;
import cn.justforchris.system.annotation.Log;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    /**
     * 新增
     *
     * @param article
     * @return
     */
    @PutMapping
    @Log
    public JsonResult insertOrUpdate(@RequestBody Article article) {
        try {
            Date date = new Date();
            article.setUpdateTime(date);
            if (article.getId() == null) {
                articleService.insertArticle(article);
            } else {
                articleService.updateArticle(article);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log
    public JsonResult deletArticle(@PathVariable Long id) {
        try {
            articleService.deleteArticle(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @Log
    public JsonResult patchDelet(@RequestBody Long[] ids) {
        try {
            articleService.patchDelete(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Article getArticle(@PathVariable Long id) {

        return articleService.getArticleById(id);

    }

    /**
     * 获取所有
     *
     * @return
     */
    @GetMapping
    public List<Article> getAll() {
        return articleService.getAll();
    }

    /**
     * 分页查询
     *
     * @param queryPage
     * @return
     */
    @PostMapping
    public PageInfo<Article> queryPage(@RequestBody articleQuery queryPage) {
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return articleService.queryPage(queryPage);

    }

    @GetMapping("/countByArticleType")
    public List<ArticleDto> countByArticleType() {
        return articleService.countByArticleType();
    }

    @GetMapping("/countByDate")
    public List<ArticleDto> countByDate() {
        return articleService.countByDate();
    }

    @GetMapping("/getRelated/{id}")
    public List<Article> getRelated(@PathVariable("id") Long id) {

        return articleService.getRelated(id);

    }

    @GetMapping("/getYear/{year}")
    public List<String> getYear(@PathVariable("year") String year) {
        List<String> i = new ArrayList<>();
        i = articleService.getYears(year);
        return i;
    }

    @GetMapping("getArticleByTime/{time}")
    public List<Article> getArticleByTime(@PathVariable("time") String time) {
        List<Article> i = new ArrayList<>();
        i = articleService.getarticleByYear(time);
        for (Object o : i) {
            System.out.println(o);

        }
        return i;
    }
//    @GetMapping("/getarticleByYear")
//    public List<Article> getarticleByYear(){
//        List i = new ArrayList<>();
//         i = articleService.getarticleByYear();
//        for (Object o : i) {
//            System.out.println(o);
//
//        }
//        return i;
//    }

    @GetMapping("/countByDate/{year}")
    public List<ArticleDto> countByDateAndYear(@PathVariable("year") String year) {
        return articleService.countByDateAndYear(year);
    }

    @GetMapping("/orderByStar")
    public List<ArticleDto> orderByStar(){
        return articleService.orderByStar();
    }

    @GetMapping("/orderByNum")
    public List<ArticleDto> orderByNum(){
        return articleService.orderByNum();
    }

    @PutMapping("/{id}")
    public JsonResult addReadCount(@PathVariable Long id){

            articleService.addReadCount(id);
            return JsonResult.success();

    }

    @GetMapping("/getArticleByType/{id}")
    public List<Article> getArticleByType(@PathVariable Long id){
        return articleService.getArticleByType(id);
    }

//    @PutMapping("/star/{id}")
//    public JsonResult addStar(@PathVariable Long id){
//        try {
//            articleService.addStar(id);
//            return JsonResult.success();
//        } catch (Exception e){
//            e.printStackTrace();
//            return JsonResult.error("000");
//        }
//    }

    @PutMapping("/updateStar/{id}")
    public JsonResult updateStar(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            return articleService.updateStar(id,request);
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return JsonResult.error("网络异常，请稍后再试！！！");
        }
    }

    @GetMapping("/initIconClass/{id}")

    public JsonResult initIconClass(@PathVariable("id")Long id,HttpServletRequest request){
        try {
            return articleService.initIconClass(id,request);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("网络异常，请稍后再试！！！");
        }
    }

}
